﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Earthwatchers Admin</title>
    <script src="jquery-1.6.2.min.js" type="text/javascript"></script>
    <script src="jquery.base64.js" type="text/javascript"></script>

    <script type="text/javascript">
        function getAuthString() {
            return "Basic " + $.base64Encode(document.getElementById("tbUsername").value + ":" + document.getElementById("tbPassword").value);
        }

        $(function () {

            $("#GenerateImagesButton").click(function () {
                if (confirm('Está seguro que desea continuar?')) {
                    $.ajax({
                        type: 'POST',
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("Authorization", getAuthString());
                        },
                        url: "../api/land/generateimages",
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        success: function (result) {
                            alert('Operación Exitosa!');
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                }
            });

            $("#GenerateGameImagesButton").click(function () {
                if (confirm('Está seguro que desea continuar?')) {
                    $.ajax({
                        type: 'POST',
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("Authorization", getAuthString());
                        },
                        url: "../api/land/generategameimages",
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        success: function (result) {
                            alert('Operación Exitosa!');
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                }
            });

            $("#ResetButton").click(function () {
                if (confirm('Está seguro que desea continuar?')) {
                    $.ajax({
                        type: 'POST',
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("Authorization", getAuthString());
                        },
                        url: "../api/land/resetlands",
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        success: function (result) {
                            alert('Operación Exitosa!');
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                }
            });

            $("#MassiveReassign").click(function () {
                if (confirm('Está seguro que desea continuar?')) {
                    $.ajax({
                        type: 'POST',
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("Authorization", getAuthString());
                        },
                        url: "../api/land/mar",
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        success: function (result) {
                            alert('Operación Exitosa!');
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                }
            });

            $(".SaveButton").click(function () {
                save();
                getMoreLands();
            });

            function save() {
                var changes = [];

                $('#landstable tr').each(function (index, item) {

                    var confirmed = null;

                    $(item).children().find("input.confirmland:checkbox").each(function () {
                        if ($(this).prop("checked")) {
                            confirmed = true;
                        }
                    });

                    $(item).children().find("input.rejectland:checkbox").each(function () {
                        if ($(this).prop("checked")) {
                            confirmed = false;
                        }
                    });

                    var observations = null;
                    $(item).children().find("input:text").each(function () {
                        observations = $(this).val();
                    });

                    var landstatus = null;
                    $(item).children().find("input.landstatus:hidden").each(function () {
                        landstatus = $(this).val();
                    });

                    var geohexKey = null;
                    $(item).children().find("input.geohexcode:hidden").each(function () {
                        geohexKey = $(this).val();
                    });

                    if (confirmed != null) {

                        if ((confirmed && landstatus == 3) || (!confirmed && landstatus == 4)) {
                            confirmed = true;
                        }
                        else {
                            confirmed = false;
                        }

                        changes.push({
                            GeohexKey: geohexKey,
                            Confirmed: confirmed,
                            LandStatus: landstatus,
                            LastUsersWithActivity: observations,
                            EarthwatcherName: $(item).find('td:eq(12)').html()
                        });
                    }
                });

                if (changes.length > 0) {
                    $.ajax({
                        type: 'POST',
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("Authorization", getAuthString());
                        },
                        traditional: true,
                        url: "../api/land/updatelandsdemand",
                        data: JSON.stringify(changes),
                        dataType: "json",
                        contentType: 'application/json; charset=utf-8',
                        success: function (result) {
                            alert('Cambios guardados ok');

                            //clear verifieds
                            $('#landstable tr td.confirmcheck input:checked').parents('tr').remove();
                            $('#landstable tr td.rejectcheck input:checked').parents('tr').remove();
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                }
            }

            $("#LoadLandsButton").click(function () {

                $('#landstable').empty();
                page = 1;

                $.ajax({
                    type: "GET",
                    data: "{\"Name\":\"" + document.getElementById("tbUsername").value + "\",\"Password\":\"" + document.getElementById("tbPassword").value + "\" }",
                    url: "../api/land/landstoconfirm/" + page + "/" + showVerifieds,
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("Authorization", getAuthString());
                    },
                    contentType: "application/json ; charset=utf-8",
                    cache: false,
                    dataType: "json",
                    success: function (result) {
                        $(result).each(function () {

                            /*
                            var checked = '';
                            if ($(this).attr('DemandAuthorities') == true) {
                                checked = 'checked';
                            }

                            var locked = '';
                            if ($(this).attr('IsLocked') == true) {
                                locked = 'checked="checked"';
                            }
                            */

                            var confirmed = $(this).attr('Confirmed');
                            var rejected = '';

                            if (confirmed == null) {
                                confirmed = '<input class="confirmland" type="checkbox" />';
                                rejected = '<input class="rejectland" type="checkbox" />';;
                            }
                            else {
                                if (confirmed == true) {
                                    confirmed = '<input type="checkbox" disabled="disabled" checked="checked" />';
                                    rejected = '<input type="checkbox" disabled="disabled" />';
                                }
                                else {
                                    confirmed = '<input type="checkbox" disabled="disabled" />';
                                    rejected = '<input type="checkbox" disabled="disabled" checked="checked" />';
                                }
                            }

                            /*
                            var statusColor = '';
                            if ($(this).attr('LandStatus') == 3) {
                                statusColor = '#ccdf68';
                            }
                            else {
                                statusColor = '#f88888';
                            }
                            */

                            var html = '';
                            html += '<td class="tdnumber"><a target="_blank" href="http://guardianes.greenpeace.org.ar/index.html?geohexcode=' + $(this).attr('GeohexKey') + '">' + $(this).attr('GeohexKey') + '</a><input class="geohexcode" type="hidden" value="' + $(this).attr('GeohexKey') + '" /></td>'
                            + '<td><img src="http://guardianes.greenpeace.org.ar/SatelliteImages/demand/' + $(this).attr('GeohexKey') + '-a.jpg" /></td>'
                            + '<td><img src="http://guardianes.greenpeace.org.ar/SatelliteImages/demand/' + $(this).attr('GeohexKey') + '-d.jpg" /></td>'
                            + '<td class="tdcheck confirmcheck" style="background-color: #ccdf68">' + confirmed + '</td>'
                            + '<td class="tdcheck rejectcheck" style="background-color: #f88888">' + rejected + '</td>'
                            + '<td class="tdcheck"><input type="text" /><input class="landstatus" type="hidden" value="' + $(this).attr('LandStatus') + '" /></td>'
                            + '<td class="tdnumber">' + $(this).attr('NegativesV') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('PositivesV') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('Negatives') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('Positives') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('OKs') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('Suspicious') + '</td>'
                            + '<td>' + $(this).attr('LastUsersWithActivity') + '</td>';

                            $('<tr></tr>').html(html).appendTo('#landstable');
                        });
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
                });
            });

            $(".filter input").click(function () {
                showVerifieds = stringToBoolean($(".filter input:checked").val());
            });
        });

        var page = 1;
        var showVerifieds = false;
        function getMoreLands() {
            page++;

            $.ajax({
                type: "GET",
                data: "{\"Name\":\"" + document.getElementById("tbUsername").value + "\",\"Password\":\"" + document.getElementById("tbPassword").value + "\" }",
                url: "../api/land/landstoconfirm/" + page + "/" + showVerifieds,
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("Authorization", getAuthString());
                },
                contentType: "application/json ; charset=utf-8",
                cache: false,
                dataType: "json",
                success: function (result) {
                    $(result).each(function () {

                        /*
                        var checked = '';
                        if ($(this).attr('DemandAuthorities') == true) {
                            checked = 'checked';
                        }

                        var locked = '';
                        if ($(this).attr('IsLocked') == true) {
                            locked = 'checked="checked"';
                        }
                        */

                        var confirmed = $(this).attr('Confirmed');
                        var rejected = '';

                        if (confirmed == null) {
                            confirmed = '<input class="confirmland" type="checkbox" />';
                            rejected = '<input class="rejectland" type="checkbox" />';;
                        }
                        else {
                            if (confirmed == true) {
                                confirmed = '<input type="checkbox" disabled="disabled" checked="checked" />';
                                rejected = '<input type="checkbox" disabled="disabled" />';
                            }
                            else {
                                confirmed = '<input type="checkbox" disabled="disabled" />';
                                rejected = '<input type="checkbox" disabled="disabled" checked="checked" />';
                            }
                        }

                        /*
                        var statusColor = '';
                        if ($(this).attr('LandStatus') == 3) {
                            statusColor = '#ccdf68';
                        }
                        else {
                            statusColor = '#f88888';
                        }
                        */

                        var html = '';
                        html += '<td class="tdnumber"><a target="_blank" href="http://guardianes.greenpeace.org.ar/index.html?geohexcode=' + $(this).attr('GeohexKey') + '">' + $(this).attr('GeohexKey') + '</a><input class="geohexcode" type="hidden" value="' + $(this).attr('GeohexKey') + '" /></td>'
                            + '<td><img src="http://guardianes.greenpeace.org.ar/SatelliteImages/demand/' + $(this).attr('GeohexKey') + '-a.jpg" /></td>'
                            + '<td><img src="http://guardianes.greenpeace.org.ar/SatelliteImages/demand/' + $(this).attr('GeohexKey') + '-d.jpg" /></td>'
                            + '<td class="tdcheck confirmcheck" style="background-color: #ccdf68">' + confirmed + '</td>'
                            + '<td class="tdcheck rejectcheck" style="background-color: #f88888">' + rejected + '</td>'
                            + '<td class="tdcheck"><input type="text" /><input class="landstatus" type="hidden" value="' + $(this).attr('LandStatus') + '" /></td>'
                            + '<td class="tdnumber">' + $(this).attr('NegativesV') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('PositivesV') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('Negatives') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('Positives') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('OKs') + '</td>'
                            + '<td class="tdnumber">' + $(this).attr('Suspicious') + '</td>'
                            + '<td>' + $(this).attr('LastUsersWithActivity') + '</td>';


                        $('<tr></tr>').html(html).appendTo('#landstable');
                    });
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        }

        function stringToBoolean(string) {
            switch (string.toLowerCase()) {
                case "true": case "yes": case "1": return true;
                case "false": case "no": case "0": case null: return false;
                default: return Boolean(string);
            }
        }
    </script>
    <style>
        body { font-family: Helvetica, Arial, "Lucida Grande", sans-serif; }

        .tdnumber { text-align: right; }

        .tdcheck { text-align: center; }

        .url { width: 300px; }

        .filter { font-size: 0.8em; margin-bottom: 10px; }
            .filter span { color: #AAA; }

        .getMore { display:inline; }

        #SaveButton2 { margin-left: 15px; margin-top: 10px;}
    </style>
</head>
<body>
    <h3>Administrator - Land</h3>
    <br />
    <br />
    username:
    <input type="text" id="tbUsername" value="" /><br />
    password:
    <input type="password" id="tbPassword" value="" />
    <br />
    <br />
    <input id="LoadLandsButton" type="button" value="Cargar Parcelas a Verificar" />
    <input id="GenerateImagesButton" type="button" value="Generar Imágenes" />
    <input id="GenerateGameImagesButton" type="button" value="Generar Imágenes del Mini Game" />
    <input id="SaveButton" class="SaveButton" type="button" value="Guardar" />
    <input id="ResetButton" type="button" value="Resetear el estado de todas las parcelas" />
    <input id="MassiveReassign" disabled="disabled" type="button" value="Reshuffle de todas las parcelas" />
    <br />
    <br />
    <div class="filter">
        <span>Filtros: </span>
        <label for="showVerifieds">No Verificados</label><input type="radio" name="showVerifieds" value="false" checked />
        <label for="showVerifieds">Verificados</label><input type="radio" name="showVerifieds" value="true" />
    </div>
    <table border="1" cellpadding="6" cellspacing="0">
        <thead>
            <tr style="background-color: #cecece">
                <td colspan="6"></td>
                <td colspan="2">Polls Verificadores</td>
                <td colspan="2">Polls</td>
                <td colspan="2">Usuarios</td>
                <td></td>
            </tr>
            <tr style="background-color: #cecece">
                <td>Parcela</td>
                <td>2008</td>
                <td>Ahora</td>
                <td style="background-color: #ccdf68">Es Verde?</td>
                <td style="background-color: #f88888">Es Rojo?</td>
                <td>Observaciones</td>
                <td style="background-color: #ccdf68">Verdes</td>
                <td style="background-color: #f88888">Rojos</td>
                <td style="background-color: #ccdf68">Verdes</td>
                <td style="background-color: #f88888">Rojos</td>
                <td style="background-color: #ccdf68">Verdes</td>
                <td style="background-color: #f88888">Rojos</td>
                <td>Primer Owner</td>
            </tr>
        </thead>
        <tbody id="landstable">
        </tbody>
    </table>
    <p class="getMore">
        <a href="javascript:void(0)" onclick="getMoreLands()">Ver mas...</a>
    </p>  <input id="SaveButton2" class="SaveButton" type="button" value="Guardar" />
    <br />
    <a href="index.html">back</a>
</body>
</html>
